package com.mingsoft.led;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mingsoft.led.util.Utils;

@Controller
public class ImageController {

	@RequestMapping(value = "/image")
	public String image(@RequestParam("index") String index,
			@RequestParam("timestmp") String timestmp,
			@RequestParam("type") String type, HttpServletResponse response) {
		FileBean fileBean = FileBean.getInstance();
		fileBean.update();
		System.out.println("Read Image File for Index: " + index);
		if (type.equals("picture")) {
			for (File f : fileBean.getPictureFiles()
					.get(Integer.parseInt(index)).listFiles()) {
				byte[] bis = Utils.getImgThumbs(f, 200, 160, fileBean
						.getPictureFiles().get(Integer.parseInt(index))
						.getName());
				if (bis != null && bis.length > 0) {
					response.setContentType("image/jpeg");
					try {
						response.getOutputStream().write(bis);
						response.getOutputStream().flush();
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					break;
				}
			}
		}
		return "dummy";
	}
}
